package com.app.service.impl;

import com.app.common.CalculateDistance;
import com.app.common.ConstantTool;
import com.app.mapper.jiuquan.FileMapper;
import com.app.mapper.jiuquan.IncubatorsMapper;
import com.app.model.FileModel;
import com.app.model.IncubatorsModel;
import com.app.service.FileService;
import com.app.service.IncubatorsService;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.app.vo.EducationVo;
import com.app.vo.IncubatorsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//孵化场地
@Service
public class IncubatorsServiceImpl extends ServiceImpl<IncubatorsMapper, IncubatorsModel> implements IncubatorsService {
    @Autowired
    private IncubatorsMapper incubatorsMapper;
    @Autowired
    private FileService fileService;

    //修改场地
    @Override
    public void updateIncubators(IncubatorsModel incubatorsModel) {
        StringBuilder idListUpdate = new StringBuilder();
        if (incubatorsModel.getUrlList() != null && incubatorsModel.getUrlList().size() > 0) {
            List<String> urlList = incubatorsModel.getUrlList();
            //将图片或者视频路径列表转化为ID列表
            idListUpdate = fileService.uriListToIdList(idListUpdate, urlList);
        }
        incubatorsModel.setFileId(idListUpdate.toString());
        incubatorsModel.setUpdateTime(CommonHelp.getNowTimestamp());
        incubatorsMapper.updateById(incubatorsModel);
    }

    //添加场地
    @Override
    public void saveIncubators(IncubatorsModel incubatorsModel) {
        StringBuilder idList = new StringBuilder();
        if (incubatorsModel.getUrlList() != null && incubatorsModel.getUrlList().size() > 0) {
            List<String> urlList = incubatorsModel.getUrlList();
            //将图片或者视频路径列表转化为ID列表
            idList = fileService.uriListToIdList(idList, urlList);
        }
        incubatorsModel.setFileId(idList.toString());
        incubatorsModel.setCreateTime(CommonHelp.getNowTimestamp());
        incubatorsMapper.insert(incubatorsModel);
    }

    @Override
    public IncubatorsVo getIncubatorsList(Integer page, Integer limit, String name, Double longitude, Double latitude, Integer area) {
        Page<IncubatorsModel> page1 = new Page<>(page, limit);
        QueryWrapper<IncubatorsModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(name)) {
            qw.like("name", name);
        }
        if(area != null && area > 0)
        {
            qw.eq("area", area);
        }
        Page<IncubatorsModel> incubatorsPage = incubatorsMapper.selectPage(page1, qw);
        List<IncubatorsModel> incubatorsList = incubatorsPage.getRecords();
        for (IncubatorsModel incubators : incubatorsList) {
            //设置返回的url路径
            List<String> urlList = new ArrayList<>();
            List<String> videoList = new ArrayList<>();
            String fileIdArr = incubators.getFileId();
            String[] split = fileIdArr.split(",");
            for (String fileIds : split) {
                int fileId = Integer.parseInt(fileIds);
                FileModel file = fileService.getById(fileId);
                if (file.getFileType() == 1) {
                    urlList.add(file.getUrl());
                }
                if (file.getFileType() == 2) {
                    videoList.add(file.getUrl());
                }
            }
            incubators.setUrlList(urlList);
            //设置返回地区的文字
            List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
            for (EducationVo education : areaVoList) {
                Integer id = education.getId();
                if (incubators.getArea() == id) {
                    incubators.setAreaText(education.getData());
                }
            }
            //计算距离
            if (longitude != null || latitude != null) {
                Double longitudeOld = incubators.getLongitude();
                Double latitudeOld = incubators.getLatitude();
                Double number = CalculateDistance.haversine(latitudeOld, longitudeOld, latitude, longitude);
                Double distance = Double.valueOf(String.format("%.1f", number));
                incubators.setDistance(distance);
            }
        }
        long count = incubatorsPage.getTotal();

        IncubatorsVo incubatorsVo = new IncubatorsVo();
        incubatorsVo.setList(incubatorsList);
        incubatorsVo.setCount((int) count);
        return incubatorsVo;

    }

    @Override
    public IncubatorsModel getUserIncubatorsById(Integer id, Double longitude, Double latitude) {
        IncubatorsModel incubators = incubatorsMapper.selectById(id);
        if (longitude != null || latitude != null) {
            Double longitudeOld = incubators.getLongitude();
            Double latitudeOld = incubators.getLatitude();
            Double number = CalculateDistance.haversine(latitudeOld, longitudeOld, latitude, longitude);
            Double distance = Double.valueOf(String.format("%.1f", number));
            incubators.setDistance(distance);
        }
        //设置返回地区的文字
        List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
        for (EducationVo education : areaVoList) {
            Integer areaId = education.getId();
            if (incubators.getArea() == areaId) {
                incubators.setAreaText(education.getData());
            }
        }
        return incubators;
    }
}
